/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.xiao.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.modules.xiao.domain.News;
import me.zhengjie.modules.xiao.domain.Swiper;
import me.zhengjie.modules.xiao.domain.dto.NewsDTO;
import me.zhengjie.modules.xiao.domain.vo.SwiperQueryCriteria;
import me.zhengjie.modules.xiao.service.NewsService;
import me.zhengjie.modules.xiao.domain.vo.NewsQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;

import me.zhengjie.utils.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author cg
* @date 2025-04-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "首页推送消息管理")
@RequestMapping("/api/news")
public class NewsController {

    private final NewsService newsService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('news:list')")
    public void exportNews(HttpServletResponse response, NewsQueryCriteria criteria) throws IOException {
        newsService.download(newsService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询首页推送消息")
    @ApiOperation("查询首页推送消息")
    @PreAuthorize("@el.check('news:list')")
    public ResponseEntity<PageResult<News>> queryNews(NewsQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(newsService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增首页推送消息")
    @ApiOperation("新增首页推送消息")
    @PreAuthorize("@el.check('news:add')")
    public ResponseEntity<Object> createNews(@Validated @RequestBody News resources){
        newsService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改首页推送消息")
    @ApiOperation("修改首页推送消息")
    @PreAuthorize("@el.check('news:edit')")
    public ResponseEntity<Object> updateNews(@Validated @RequestBody News resources){
        newsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除首页推送消息")
    @ApiOperation("删除首页推送消息")
    @PreAuthorize("@el.check('news:del')")
    public ResponseEntity<Object> deleteNews(@RequestBody List<Integer> ids) {
        newsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("查询轮播图API")
    @ApiOperation("查询轮播图API")
    @AnonymousGetMapping(value = "/top5")
    public Result<List<NewsDTO>> top5(NewsQueryCriteria criteria){
        return Result.success(newsService.top5(criteria));
    }
}